package myapp.com.xm.myapplication.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import myapp.com.xm.myapplication.R;
import myapp.com.xm.myapplication.RecyclerView1;

public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int ONE_ITEM = 1;
    public static final int TWO_ITEM = 2;
    public static final int THREE_ITEM = 3;

    private Context context;

    public Adapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        RecyclerView.ViewHolder viewHolder;
        if (viewType == ONE_ITEM) {
            View view = layoutInflater.inflate(R.layout.list_item_1, parent, false);
            viewHolder = new ViewHoder1(view);
        } else if (viewType == TWO_ITEM) {
            View view = layoutInflater.inflate(R.layout.list_item_2, parent, false);
            viewHolder = new ViewHoder2(view);
        } else {
            View view = layoutInflater.inflate(R.layout.list_item_3, parent, false);
            viewHolder = new ViewHoder3(view);
        }
        return viewHolder;
    }

    private float y;
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHoder1) {

        } else if (holder instanceof ViewHoder2) {

        } else {

            ((ViewHoder3) holder).recyclerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction()==MotionEvent.ACTION_DOWN || event.getAction()==MotionEvent.ACTION_MOVE){
                        LinearLayoutManager layoutManager = (LinearLayoutManager) ((ViewHoder3) holder).recyclerView.getLayoutManager();
                        int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                        if(event.getAction()==MotionEvent.ACTION_DOWN){
                            y=event.getY();
                        }else{
                            if(y<event.getY() && firstCompletelyVisibleItemPosition == 0){
                                EventBus.getDefault().post("0");
                                v.getParent().requestDisallowInterceptTouchEvent(false);
                            }else{
                                EventBus.getDefault().post("-1");
                                v.getParent().requestDisallowInterceptTouchEvent(true);
                            }
                            y=event.getY();
                        }

                    }else{
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                    }
                    return false;
                }
            });
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position < getItemCount() - 2) {
            return ONE_ITEM;
        } else if (position == getItemCount() - 2) {
            return TWO_ITEM;
        } else {
            return THREE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return 20;
    }


    class ViewHoder1 extends RecyclerView.ViewHolder {

        public ViewHoder1(@NonNull View itemView) {
            super(itemView);
        }
    }

    class ViewHoder2 extends RecyclerView.ViewHolder {
        private TextView tv_tab;

        public ViewHoder2(@NonNull View itemView) {
            super(itemView);
            tv_tab = itemView.findViewById(R.id.tv_tab);
        }
    }

    class ViewHoder3 extends RecyclerView.ViewHolder {
        public RecyclerView1 recyclerView;
        public Adapter2 adapter2;

        public ViewHoder3(@NonNull View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.list_item);
            adapter2 = new Adapter2(context);
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(adapter2);
        }
    }


}
